﻿using System;
using System.Data;
using System.Linq;
using Model;

namespace BLL
{
    public class Sys_Users_Logs_BLL
    {
        ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

        public IQueryable<Sys_Users_Logs> Get()
        {
            return _db.Sys_Users_Logs.OrderByDescending(a => a.AddDate);
        }

        public Sys_Users_Logs Get(Guid? Id)
        {
            var item = new Sys_Users_Logs();

            if (Id != null)
            {
                item = _db.Sys_Users_Logs.Single(a => a.Id.Equals(Id.Value));
            }
            return item;
        }

        public void Save(Guid? Id, ref Sys_Users_Logs item)
        {
            ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

            if (Id == null)
            {
                item.Id = Guid.NewGuid();
                _db.Sys_Users_Logs.AddObject(item);
            }
            else
            {
                _db.Sys_Users_Logs.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            }

            _db.SaveChanges();
        }

        public void DeleteObject(Guid Id)
        {
            var item = new Sys_Users_Logs() { Id = Id };
            _db.Sys_Users_Logs.Attach(item);
            _db.ObjectStateManager.ChangeObjectState(item, EntityState.Deleted);
            _db.SaveChanges();
        }
    }
}
